using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fads .Model;

namespace fads .DAL
{
	public class page_operateDAL
	{
		public int AddNew(page_operate model)
		{
			object obj = SqlHelper.ExecuteScalar(
				"INSERT INTO page_operate(page_id,web_url,request_url,page_operate_code,page_operate_name,description,operate_type,is_field_operate,request_type,icon_url,create_time,update_time) VALUES (@page_id,@web_url,@request_url,@page_operate_code,@page_operate_name,@description,@operate_type,@is_field_operate,@request_type,@icon_url,@create_time,@update_time);SELECT @@identity"
				,new SqlParameter("@page_id", model.page_id)
				,new SqlParameter("@web_url", model.web_url)
				,new SqlParameter("@request_url", model.request_url)
				,new SqlParameter("@page_operate_code", model.page_operate_code)
				,new SqlParameter("@page_operate_name", model.page_operate_name)
				,new SqlParameter("@description", model.description)
				,new SqlParameter("@operate_type", model.operate_type)
				,new SqlParameter("@is_field_operate", model.is_field_operate)
				,new SqlParameter("@request_type", model.request_type)
				,new SqlParameter("@icon_url", model.icon_url)
				,new SqlParameter("@create_time", model.create_time)
				,new SqlParameter("@update_time", model.update_time)
			);
			return Convert.ToInt32(obj);
		}

		public bool Delete(int id)
		{
			int rows = SqlHelper.ExecuteNonQuery("DELETE FROM page_operate WHERE id = @id", new SqlParameter("@id", id));
			return rows > 0;
		}

		public bool Update(page_operate model)
		{
			string sql = "UPDATE page_operate SET page_id=@page_id,web_url=@web_url,request_url=@request_url,page_operate_code=@page_operate_code,page_operate_name=@page_operate_name,description=@description,operate_type=@operate_type,is_field_operate=@is_field_operate,request_type=@request_type,icon_url=@icon_url,create_time=@create_time,update_time=@update_time WHERE id=@id";
			int rows = SqlHelper.ExecuteNonQuery(sql
				, new SqlParameter("@id", model.id)
				, new SqlParameter("@page_id", model.page_id)
				, new SqlParameter("@web_url", model.web_url)
				, new SqlParameter("@request_url", model.request_url)
				, new SqlParameter("@page_operate_code", model.page_operate_code)
				, new SqlParameter("@page_operate_name", model.page_operate_name)
				, new SqlParameter("@description", model.description)
				, new SqlParameter("@operate_type", model.operate_type)
				, new SqlParameter("@is_field_operate", model.is_field_operate)
				, new SqlParameter("@request_type", model.request_type)
				, new SqlParameter("@icon_url", model.icon_url)
				, new SqlParameter("@create_time", model.create_time)
				, new SqlParameter("@update_time", model.update_time)
			);
			return rows > 0;
		}

		public page_operate Get(int id)
		{
			DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM page_operate WHERE id=@id", new SqlParameter("@id", id));
			if (dt.Rows.Count > 1)
			{
				throw new Exception("more than 1 row was found");
			}
			if (dt.Rows.Count <= 0)
			{
				return null;
			}
			DataRow row = dt.Rows[0];
			page_operate model = ToModel(row);
			return model;
		}

		private static page_operate ToModel(DataRow row)
		{
			page_operate model = new page_operate();
			model.id = (object)row["id"];
			model.page_id = (object)row["page_id"];
			model.web_url = (string)row["web_url"];
			model.request_url = (string)row["request_url"];
			model.page_operate_code = (string)row["page_operate_code"];
			model.page_operate_name = (string)row["page_operate_name"];
			model.description = (string)row["description"];
			model.operate_type = (int)row["operate_type"];
			model.is_field_operate = (int)row["is_field_operate"];
			model.request_type = (int)row["request_type"];
			model.icon_url = (string)row["icon_url"];
			model.create_time = (object)row["create_time"];
			model.update_time = (object)row["update_time"];
			return model;
		}

		public IEnumerable<page_operate> ListAll()
		{
			List<page_operate> list = new List<page_operate>();
			DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM page_operate");
			foreach (DataRow row in dt.Rows)
			{
				list.Add(ToModel(row));
			}
			return list;
		}
	}
}
